#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#


class FederatedPacketRouting:
    def __init__(
            self,
            task_id: str,
            src_role: str = None,
            src_node_id: str = None,
            send_topic: str = None,
            receive_topic: str = None,
            dest_role: str = None,
            dest_node_id: str = None,
            dest_send: str = None,
            dest_receive: str = None,
    ):
        self.task_id = task_id
        self.src_role = src_role
        self.src_node_id = src_node_id
        self.send_topic = send_topic
        self.receive_topic = receive_topic
        self.dest_role = dest_role
        self.dest_node_id = dest_node_id
        self.dest_send = dest_send
        self.dest_receive = dest_receive
        self.retries = 0

    def __str__(self):
        return f"task id: {self.task_id}, send topic: {self.send_topic}, receive topic: {self.receive_topic}, src node id: {self.src_node_id}, dest node: {self.dest_node_id}"

    def to_dict(self) -> dict:
        d = {
            "task-id": self.task_id,
            "src-role": self.src_role,
            "src-node-id": self.src_node_id,
            "src-send-topic": self.send_topic,
            "src-receive-topic": self.receive_topic,
            "dest-role": self.dest_role,
            "dest-node-id": self.dest_node_id,
            "dest-send-topic": self.dest_send,
            "dest-receive-topic": self.dest_receive,
            "retries": self.retries,
        }
        return d

    @classmethod
    def from_dict(cls, d: dict):
        return FederatedPacketRouting(
            task_id=d["task-id"],
            src_role=d["src-role"],
            src_node_id=d["src-node-id"],
            send_topic=d["src-send-topic"],
            receive_topic=d["src-receive-topic"],
            dest_role=d["dest-role"],
            dest_node_id=d["dest-node-id"],
            dest_send=d["dest-send-topic"],
            dest_receive=d["dest-receive-topic"],
        )
